package com.lgy.springboot_example.controller;

import com.lgy.springboot_example.entity.OrderInfo;
import com.lgy.springboot_example.mapper.OrderInfoMapper;
import com.lgy.springboot_example.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "Excel操作")
@RequestMapping("excel")
@RestController
public class ExcelController {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private SimpleDateFormat sdf;

    @ApiOperation("导出excel")
    @GetMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) {
        List<OrderInfo> list = orderInfoMapper.selectAll();
        String[] title = {"订单Id", "订单名称", "创建时间", "订单状态"};
        String fileName = "订单信息_" + new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date()) + ".xls";
        String sheetName = "订单信息";
        //二维数组
        String[][] data = new String[list.size()][title.length];
        for (int i = 0; i < data.length; i++) {
            OrderInfo oi = list.get(i);
            data[i][0] = oi.getOrderId().toString();
            data[i][1] = oi.getOrderName();
            data[i][2] = sdf.format(oi.getCreateTime());
            data[i][3] = oi.getStatus() == 1 ? "已支付" : "其他"; // Java 三元表达式
        }
        ExcelUtil.getHSSFwb(fileName, sheetName, title, data, response);
    }
}
